﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class ContactUsPageTemplate :TemplateBase<ContactUsPage>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MsgLiteral.Visible = false;
        }


        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            if (ValidateRegistration())
            {
                var sb = new StringBuilder();
                string emailBody = string.Empty;
                var node = NodeLocator.GetNodeOfExactType<EmailTemplates>();
                if (node != null)
                {
                    emailBody = node.ContactUsTemplate.Replace("{name}", author.Value);
                    emailBody = emailBody.Replace("{email}", email.Value);
                    emailBody = emailBody.Replace("{message}", text.Value);
                }
                else
                {
                    sb.AppendLine();
                    sb.AppendLine("Name: " + author.Value);
                    sb.AppendLine("Email: " + email.Value);
                    sb.AppendLine();
                    sb.AppendLine("Message: " + text.Value);
                    emailBody = sb.ToString();
                }
                var mailMessage = new MailMessage { Subject = subject.Value, Body = emailBody , From =  new MailAddress(CurrentContent.SmtpSender), IsBodyHtml=true };
                LoadRecepientEmails(mailMessage.To);
                var smtpClient = new SmtpClient
                {
                    Host = CurrentContent.SmtpHost,
                    Port = int.Parse(CurrentContent.SmtpPort),                  
                    EnableSsl = CurrentContent.SmtpEnableSSL,
                    Credentials = new NetworkCredential(CurrentContent.SmtpUsername,CurrentContent.SmtpPassword)
                };                
                sb = new StringBuilder();
                var msgFormat = "<p class=\"{0}\">{1}</p>";

                try { 
                    smtpClient.Send(mailMessage);
                    sb.AppendLine(string.Format(msgFormat, "msg done", "Successfully Sent!, Thank you"));
                    clearControls();
                }
                catch {
                    sb.AppendLine(string.Format(msgFormat, "msg error", "Sorry,There was an error sending your mail."));
                }
                                              
                MsgLiteral.Visible = true;
                MsgLiteral.Text = sb.ToString();
                
            }
        }

        private void LoadRecepientEmails(MailAddressCollection toEmails)
        {

            toEmails.Add(CurrentContent.SmtpUsername);

            //if (!string.IsNullOrEmpty(toEmailsCsv))
            //{
            //    string[] emails = toEmailsCsv.Split(';');
            //    foreach (string email in emails)
            //    {
            //        toEmails.Add(email);
            //    }
            //}
            //else
            //{
            //    toEmails.Add(DefaultAdministratorEmail);
            //}
        }

        void clearControls()
        {
            author.Value = string.Empty;
            email.Value = string.Empty;
            subject.Value = string.Empty;
            text.Value = string.Empty;
        }

        bool ValidateRegistration()
        {

            var sb = new StringBuilder();
            var msgFormat = "<p class=\"{0}\">{1}</p>";
            var isValid = true;
            //

            if (string.IsNullOrEmpty(author.Value))
            {
                isValid = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Name is required"));
            }
            if (string.IsNullOrEmpty(email.Value))
            {
                isValid = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Email is required"));
            }
            if (string.IsNullOrEmpty(subject.Value))
            {
                isValid = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Subject is required."));
            }
            if (!isValid)
            {
                MsgLiteral.Visible = true;
                MsgLiteral.Text = sb.ToString();
            }
            return isValid;
        }

    }
}